<?php
defined('_VISIT_URL_') or exit('No direct script access allowed');

class recommend {

	//推送首页的专属推荐信息
	public function pushToExclusive() {
		if (empty(getPost('infoid'))) {
			return util::getValidError('信息ID格式错误');
		}

		if (!valid::isNum(getPost('tabtype'))) {
			return util::getValidError('推荐分类格式错误');
		}

		if (empty(getPost('sortation')) || valid::isWicked(getPost('sortation'))) {
			return util::getValidError('栏目分类格式异常');
		}

		if (empty(getPost('datatype')) || valid::isWicked(getPost('datatype'))) {
			return util::getValidError('数据分类格式异常');
		}

		$table = '';
		$idField = '';
		$flag = '';

		if (getPost('datatype') == 'electronic') {
			$table = 'electronic';
			$idField = 'electronicid';
		}
		else if (getPost('datatype') == 'housecraft') {
			$table = 'housecraft';
			$idField = 'housecraftid';
		}
		else if (getPost('datatype') == 'other') {
			$table = 'other';
			$idField = 'otherid';
		}
		else {
			return util::getDataError('数据分类错误');
		}

		$rst = new sqlcon();

		$new = $rst->getInfo('*', $table, $idField . " = '" . getPost('infoid') . "' and status = 1");
		if (empty($new)) {
			return util::getDataError('该信息不存在');
		}

		$old = $rst->getInfo('id', 'recommend', "infoid = '" . getPost('infoid') . "' and tabtype = " . getPost('tabtype') . " and datatype = '" . getPost('datatype') . "'");
		if (empty($old)) {
			$flag = 'add';
		}
		else {
			$flag = 'edit';
		}

		if ($flag == 'add') {
			$rst->addCollect('infoid', getPost('infoid'));
			$rst->addCollect('title', $new['title']);
			$rst->addCollect('tabtype', getPost('tabtype'));
			$rst->addCollect('sortation', getPost('sortation'));
			$rst->addCollect('datatype', getPost('datatype'));
			$rst->addCollect('feature', $new['feature']);
			$rst->addCollect('area', $new['area']);
			$rst->addCollect('smallpic', $new['smallpic']);
			$rst->addCollect('status', $new['status']);
			$rst->addCollect('sort', $new['sort']);
			$rst->addCollect('expiretime', $new['expiretime']);
			$rst->insertInfo('recommend');
		}
		else {
			$rst->addCollect('title', $new['title']);
			$rst->addCollect('feature', $new['feature']);
			$rst->addCollect('area', $new['area']);
			$rst->addCollect('smallpic', $new['smallpic']);
			$rst->addCollect('status', $new['status']);
			$rst->addCollect('sort', $new['sort']);
			$rst->addCollect('expiretime', $new['expiretime']);
			$rst->addCollect('id', $old['id']);
			$rst->updateInfo('recommend');
		}

		$result = util::getSuccess('success');
		$result['flag'] = $flag;
		return $result;
	}

	//获取推荐信息的分类列表
	public function getSortationList() {
		$rst = new sqlcon();
		$list = $rst->getList('distinct sortation as text, sortation as value', 'recommend', 'id != 0');

		$result = util::getSuccess('success');
		$result['sortationList'] = $list;

		return $result;
	}

	//获取推荐信息列表
	public function getRecommendList() {
		$status = getPost('status');
		$tabtype = getPost('tabtype');
		$sortation = getPost('sortation');
		$page = getPost('page', '1');
		$permission = getPost('permission');

		$queryitem = '';
		if ($status != '')
			$queryitem .= ' and status = ' . $status;
		if ($tabtype != '')
			$queryitem .= ' and tabtype = ' . $tabtype;
		if ($sortation != '')
			$queryitem .= " and sortation = '" . $sortation . "'";

		$limititem = ' limit ' . ($page - 1) * 50 . ', 50';
		$sortOption = 'expiretime desc, id desc';

		$rst = new sqlcon();

		$info = $rst->getInfo('count(*) as num', 'recommend', 'id != 0' . $queryitem);

		$list = $rst->getList('infoid, title, tabtype, sortation, feature, area, smallpic, status', 'recommend', 'id != 0' . $queryitem, $sortOption . $limititem);
		for ($i = 0; $i < count($list); $i++) {
			//格式化列表小图路径
        	if (!empty($list[$i]['smallpic'])) {
                $list[$i]['smallpic'] = _HTTP_PATH_ . $list[$i]['smallpic'];
            }
		}

		$result = util::getSuccess('success');
	    $result['listCount'] = $info['num'];
	    $result['allPages'] = util::getListPage($info['num'], 50);
	    $result['recommendList'] = $list;
	    $result['rowCount'] = 50;
	    if ($permission == 'yes') {
	    	$result['permission'] = util::getOperateItem(getPost('menuid'));
	    }
		return $result;
	}

	//删除推荐信息
	public function deleteInfo() {
		if (empty(getPost('infoid'))) {
			return util::getValidError('ID格式错误');
		}

		$rst = new sqlcon();

		$rst->addCollect('infoid', getPost('infoid'));
        $rst->deleteInfo('recommend');

        return util::getSuccess('success');
	}
	
}